﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Win.CSkin.Test
{
    public partial class ExtendTextBox : TextBox
    {
        public ExtendTextBox()
        {
            InitializeComponent();
        }

        public ExtendTextBox(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        /// <summary>
        /// 允许输入类型
        /// </summary>
        [Browsable(true)]
        [Description("允许输入类型")]
        public AllowType AllowType { get; set; } = AllowType.All;

        protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
        {
            //设置此属性为true 基方法将不再处理此KeyPress事件
            e.Handled = true;

            if (AllowType == AllowType.Number)
            {
                //如果输入是0-9 交由基方法处理 允许输入
                if (e.KeyChar >= '0' && e.KeyChar <= '9')
                    e.Handled = false;
            }
            else if (AllowType == AllowType.Letter)
            {
                //如果输入是A-Z或a-z 交由基方法处理 允许输入
                if (e.KeyChar >= 'A' && e.KeyChar <= 'z')
                    e.Handled = false;
            }
            else if (AllowType == AllowType.All)
            {
                e.Handled = false;
            }
            base.OnKeyPress(e);
        }
    }

    public enum AllowType
    {
        Number,
        Letter,
        All
    }

}
